<?php

abstract class Static_Backend_Utils 
{
    
    /**
    * Return active static contents entry id
    * 
    * @return integer
    */
    static public function active() 
    {
        return (int)Application::getRequest()->get('id', 
            count($entries = self::getAll()) ? current(current($entries)) : 0);
    }
    
    /**
    * Return static contents list
    * 
    * @return array
    */
    static public function getAll() 
    {
        static $cach;
        
        if (is_null($cach)) {
            $query = self::_query();
            $query .= '
              ORDER BY 
                       `title` ASC
                ';
            $cach = database::fetchAll($query);
        }
        
        return $cach;
    }
    
    
    /**
    * Return contents specified
    * 
    * @param integer $id
    * @return array
    */
    static public function getSpecified($id) 
    {
        $query = self::_query();
        $query .= '
             WHERE 
                   `id` = ' . (int)$id;
        
        return database::fetchRow($query);
    }
    
    /**
    * Return contents query
    * 
    * @return string
    */
    static private function _query() 
    {
        return 
        '
            SELECT 
                   `c`.`id`,  
                   `c`.`title`, 
                   `c`.`contents`
              FROM 
                   `system_contents` `c` 
              JOIN 
                   `static` `s` ON `s`.`contents_id` = `c`.`id`
        ';
    }
    
}